package com.zky.basic.service.impl;

import com.zky.basic.dto.PhoneDto;
import com.zky.basic.exception.BusinessException;
import com.zky.basic.service.IVerifyCodeService;
import com.zky.user.domain.User;
import com.zky.user.mapper.UserMapper;
import com.zky.util.SmsUtils;
import com.zky.util.StrUtils;
import com.zky.util.VerifyCodeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.TimeUnit;
@Service
@Transactional
public class VerifyCodeServiceImpl implements IVerifyCodeService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserMapper userMapper;

    @Override
    public String getImageCode(String key) {
        String code = StrUtils.getComplexRandomString(4);
        redisTemplate.opsForValue().set(key,code,5, TimeUnit.MINUTES);
        return VerifyCodeUtils.verifyCode(115,40,code);

    }

    @Override
    public void getPhoneCode(PhoneDto phoneDto) {
        //校验空值
        if(StringUtils.isEmpty(phoneDto.getImageCode()) || StringUtils.isEmpty(phoneDto.getPhone())){
            throw new BusinessException("信息不能为空");
        }
        //校验手机号是否注册过
        User user = userMapper.findByPhone(phoneDto.getPhone());
        if (user!=null){
            throw new BusinessException("当前手机号已注册，请登录，如果忘记密码，请联系管理员");
        }
        //校验图形验证码是否过期
        Object imageObj =  redisTemplate.opsForValue().get(phoneDto.getImageCodeKey());
        if (imageObj==null){
            throw new BusinessException("验证码过期");
        }
        //校验图形验证码是否正确
        if (!phoneDto.getImageCode().equalsIgnoreCase(imageObj.toString())){
            throw new BusinessException("验证码不正确");
        }
        //获取手机短信验证码【业务键：电话号码 -  手机验证码：时间戳】
        //通过key从redis获取手机验证码

        Object phoneObj = redisTemplate.opsForValue().get("register:" + phoneDto.getPhone());
        String phoneCode = null;
        if (phoneObj==null){
           //为空，第一次发送或者过期，重新发送
           phoneCode =  StrUtils.getRandomString(4);
        }else {
            //不为空，手机验证码是否过重发时间


            long now = System.currentTimeMillis();
            Long old = Long.parseLong(phoneObj.toString().split(":")[1]);
            if (now - old <1*60*1000){
                //没有过重发时间，违规操作
                throw  new BusinessException("请勿重复操作");
            }else {
                //过了重发时间，重新发送
                phoneCode = phoneObj.toString().split(":")[0];
            }

        }
        //刷新过期时间，
        redisTemplate.opsForValue().set("register:" + phoneDto.getPhone(),phoneCode +":"+ System.currentTimeMillis(),3,TimeUnit.MINUTES);

        //调用工具类发送短信
       // SmsUtils.sendSms(phoneDto.getPhone(),"你的验证码是："+phoneCode+",请在三分钟类使用");
        System.out.println("你的验证码是："+phoneCode+",请在三分钟类使用");
    }

    @Override
    public void binderSmsCode(PhoneDto phoneDto) {
        //校验空值
        if( StringUtils.isEmpty(phoneDto.getPhone())){
            throw new BusinessException("信息不能为空");
        }

        //获取手机短信验证码【业务键：电话号码 -  手机验证码：时间戳】
        //通过key从redis获取手机验证码

        Object phoneObj = redisTemplate.opsForValue().get("phone:" + phoneDto.getPhone());
        String phoneCode = null;
        if (phoneObj==null){
            //为空，第一次发送或者过期，重新发送
            phoneCode =  StrUtils.getRandomString(4);
        }else {
            //不为空，手机验证码是否过重发时间


            long now = System.currentTimeMillis();
            Long old = Long.parseLong(phoneObj.toString().split(":")[1]);
            if (now - old <1*60*1000){
                //没有过重发时间，违规操作
                throw  new BusinessException("请勿重复操作");
            }else {
                //过了重发时间，重新发送
                phoneCode = phoneObj.toString().split(":")[0];
            }

        }
        //刷新过期时间，
        redisTemplate.opsForValue().set("phone:" + phoneDto.getPhone(),phoneCode +":"+ System.currentTimeMillis(),3,TimeUnit.MINUTES);

        //调用工具类发送短信
        // SmsUtils.sendSms(phoneDto.getPhone(),"你的验证码是："+phoneCode+",请在三分钟类使用");
        System.out.println("你的验证码是："+phoneCode+",请在三分钟类使用");
    }
}